/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Persistencia;

import java.sql.*;
import java.util.Iterator;
import java.util.List;
/**
 *
 * @author Jaqueline Ishiyama
 */
public class CRUD {
    
    public static void adiciona(String tabela, String campos, String valores){
        try {
            String sql = "insert into "+tabela+"("+campos+") values("+valores+")";
            
            Connection connection = ConnectionFactory.getConnection();           
            PreparedStatement preparedStatement = connection.prepareStatement(sql);                        
            preparedStatement.executeUpdate();                        
            preparedStatement.close();                        
            connection.close();                        
        } catch (SQLException e) {                                      
            throw  new RuntimeException(e);
        }        
    }
    
    public static void atualiza(String tabela, List<String> campos, List<String> valores, String where){
        StringBuilder sb = new StringBuilder();
        Iterator<String> itV = valores.iterator();
        for (Iterator<String> itC = campos.iterator(); itC.hasNext();) {
            String itCstring = itC.next();
            String itVstring = itV.next();
            sb.append(" ").append(itCstring).append(" = ").append(itVstring).append(",");                        
        }
        String sql = "update " + tabela + " set " + sb.toString().substring(0, sb.toString().length()-1);
        sql += " " + where;
        try {
            Connection connection = ConnectionFactory.getConnection();           
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.execute();
            preparedStatement.close();
            connection.close();                       
        } catch (SQLException e) {
            throw  new RuntimeException(e);
        }
    }
    
    public static void deleta(String tabela, String campo, List<Integer> ids){
        try {
            StringBuilder sb = new StringBuilder();
            for (Iterator<Integer> it = ids.iterator(); it.hasNext();) {                
                Integer integer = it.next();
                sb.append(integer.intValue()).append(",");
            }            
            String valores = sb.toString().substring(0, sb.length()-1);
            String sql = "delete from " + tabela + " where " +campo+ " in (" + valores + ")";              
            
            Connection connection = ConnectionFactory.getConnection();           
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.execute();
            preparedStatement.close();
            connection.close();                       
        } catch (SQLException e) {
            throw  new RuntimeException(e);
        }
    }
    
    public static ResultSet consulta(String sql){
        try {           
            Connection connection = ConnectionFactory.getConnection();           
            PreparedStatement preparedStatement = connection.prepareStatement(sql);            
            return preparedStatement.executeQuery();            
        } catch (SQLException e) {                                      
            throw  new RuntimeException(e);
        }
    }    
}
